package com.ys.peony.car300.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.ys.peony.R;
import com.ys.peony.base.contract.BasicActivity;
import com.ys.peony.base.contract.CarPledgeContract;
import com.ys.peony.car300.BaoDanFragment;
import com.ys.peony.car300.CarDataManager;
import com.ys.peony.car300.PledgeFragment;
import com.ys.peony.model.domain.CarDetailModel;
import com.ys.peony.presenter.CarPledgePresenter;
import com.ys.peony.ui.activity.CarPledgeActivity;
import com.ys.peony.ui.activity.WebViewActivity;
import com.ys.peony.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class CarSeeActivity extends BasicActivity<CarPledgeContract.ICarUpdateView, CarPledgePresenter> implements CarPledgeContract.ICarUpdateView {


    @BindView(R.id.select_sort)
    RadioGroup select_sort;
    @BindView(R.id.car_sort)
    RadioButton car_sort;
    @BindView(R.id.baodan_sort)
    RadioButton baodan_sort;

    @BindView(R.id.id_viewpager)
    ViewPager mViewPager;

    @BindView(R.id.titleback)
    ImageView titleback;
    @BindView(R.id.look)
    ImageView look;
    private List<Fragment> mList = new ArrayList<>();
    private int mPosition; //记录当前车辆下标

    @Override
    protected int getContentResId() {
        return R.layout.activity_car_see;
    }


    @Override
    protected void initView() {
        initData();
        initListener();
        initAdapter();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected CarPledgePresenter initPresenter() {
        return new CarPledgePresenter();
    }

    private void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            int position = bundle.getInt("position");
            mPosition = position;
            PledgeFragment pledgeFragment = new PledgeFragment();
            BaoDanFragment baoDanFragment = new BaoDanFragment();
            pledgeFragment.setDataIndex(position);
            pledgeFragment.setType(CarPledgeActivity.TYPE_OPERATE_SEE);
            baoDanFragment.setDataIndex(position);
            baoDanFragment.setType(CarPledgeActivity.TYPE_OPERATE_SEE);
            mList.add(pledgeFragment);
            mList.add(baoDanFragment);
        }

    }

    private void initAdapter() {
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mList.get(position);
            }

            @Override
            public int getCount() {
                return mList.size();
            }
        });
    }

    private void initListener() {

        select_sort.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i) {
                    case R.id.car_sort:
                        mViewPager.setCurrentItem(0);
                        hideInput();
                        break;
                    case R.id.baodan_sort:
                        mViewPager.setCurrentItem(1);
                        hideInput();
                        break;
                }
            }
        });

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                changeBtn(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        titleback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hideInput();
                finish();
            }
        });
        look.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CarDetailModel carDetailModel = CarDataManager.getInstance().getCarDetailModel();
                if (carDetailModel != null) {
                    List<CarDetailModel.BusiMortgageVoBean.VehicleListBean> vehicleList = carDetailModel.getBusiMortgageVo().getVehicleList();
                    List<List<CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean>> lists = carDetailModel.getBusiMortgageVo().getBusivehicleandslipvoList();
                    if (lists != null && lists.size() > mPosition) {
                        CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean busivehicleandslipvoListBeen = lists.get(mPosition).get(0);
                        String url = busivehicleandslipvoListBeen.getReport_url();
                        if(TextUtils.isEmpty(url)) {
                            ToastUtils.showToast("该车辆无定价报告");
                        } else {
                            Bundle bundle = new Bundle();
                            bundle.putString(WebViewActivity.PARAM_URL, url);
                            skipToTargetPage(WebViewActivity.class, bundle);
                        }
                    }
                }
            }
        });
    }


    private void changeBtn(int position) {
        switch (position) {
            case 0:
                car_sort.setChecked(true);
                hideInput();
                break;
            case 1:
                baodan_sort.setChecked(true);
                hideInput();
                break;
        }
    }

    @Override
    public void onFail(String msg) {
        ToastUtils.showToast(msg);
    }

    @Override
    public void onSuccess(int code) {
        switch (code) {
            case CarPledgeContract.DELETE_CAR:
                ToastUtils.showToast("删除成功");
                finish();
                break;
        }
    }
}
